# Create your views here.
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View
from io import BytesIO





# 注册功能
from common.captcha_4char.captcha import veri_code


def register(request):
    # 5.1 判断数据提交方式
    if request.method == "POST":
        # 5.2 获取用户数据
        post_obj = request.POST
        username = post_obj.get('username')
        password = post_obj.get('password')
        email = post_obj.get('email')
        # 5.3 操作User表
        from django.contrib.auth.models import User
        # User.objects.create(username=username,password=password,email=email)
        User.objects.create_user(username=username, password=password, email=email)
        return HttpResponseRedirect(reverse('captcha_demo_app:login'))
    return render(request, 'captcha_study/register.html')


# 登录功能
class Lgoin(View):
    def get(self, request):
        error_msg = request.session.get('error_msg')

        if error_msg:
            del request.session['error_msg']
        return render(request, './captcha_study/login.html', context={'error_message': error_msg})

    def post(self, request):
        post_obj = request.POST
        username = post_obj.get('username')
        password = post_obj.get('password')
        checkcode = post_obj.get('checkcode')
        right_code = request.session.get('check_code')
        # 校验验证码是否正确
        if checkcode.lower()!=right_code.lower():
            request.session['error_msg']='验证码不正确'
            return redirect('captcha_demo_app:login')
        from django.contrib import auth
        # 校验用户名和密码是否正确
        User_obj = auth.authenticate(username=username, password=password)
        print('User_obj-----', User_obj)
        print('username-----', username)
        print('password-----', password)

        if User_obj:
            # 保存session
            auth.login(request, User_obj)
            return redirect('captcha_demo_app:index')
        else:
            error_msg = '用户名或密码错误'
            request.session['error_msg'] = error_msg
            return redirect('captcha_demo_app:login')
            # return render(request, './captcha_study/login.html', locals())


# 注销功能
def logout(request):
    auth.logout(request)
    return redirect('captcha_demo_app:login')


# 修改密码功能
class SetPassword(View):
    def post(self,request):
        post_obj = request.POST
        old_password = post_obj.get('old_password')
        new_password= post_obj.get('new_password')
        confirm_password = post_obj.get('confirm_password')
        print('request.user',request.user )
        if not confirm_password==new_password:
            error_msg={'confirm_password':'两次密码不一样'}
            return render(request,'./captcha_study/set_password.html',{'error_msg':error_msg})
        flag=request.user.check_password(old_password)
        print('flag',flag )
        if not flag:
            error_msg = {'old_password': '密码不正确'}
            return render(request, './captcha_study/set_password.html', {'error_msg': error_msg})
        request.user.set_password(new_password)
        request.user.save()  # 一定要保存
        return HttpResponseRedirect(reverse('captcha_demo_app:index'))


    def get(self,request):
         return render(request, './captcha_study/set_password.html')

# 登陆之后可以查看的页面
# @login_required
def index(request):
    return render(request, 'captcha_study/index.html')

def captcha_img(request):
    stream = BytesIO()
    img, code = veri_code()
    img.save(stream, 'PNG')
    request.session['check_code'] = code
    print(code)
    return HttpResponse(stream.getvalue())